/*
 * Copyright (C) 2009 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */

package com.kindac.together.widget;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.kindac.together.R;
import com.kindac.together.common.Kin;
import com.kindac.together.db.KinDB;
import com.kindac.together.uitl.Utils;

/**
 * @author Kindac
 * @time 2011-6-21 上午01:26:51
 */
public class LolPostActivity extends Activity implements OnClickListener {

    private Intent   i;
    private EditText edit;
    private TextView post;
    private KinDB    db;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.post);

        init();

        try {
            i = getIntent();
            if (Kin.ADD.equals(i.getAction())) newinit();
            else update();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // @Override
    public void onAttachedToWindow() {
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
        super.onAttachedToWindow();
    }

    private void init() {

        Button save = (Button) findViewById(R.id.save);
        save.setOnClickListener(this);

        edit = (EditText) findViewById(R.id.edit);

        post = (TextView) findViewById(R.id.postText);

        db = KinDB.getInstance(LolPostActivity.this);
    }

    private void newinit() {
        Button look = (Button) findViewById(R.id.look);
        look.setVisibility(View.GONE);
        String content = getResources().getString(R.string.text_demo).replace(",", "\n");
        post.setText(content);
        edit.setText(null);
    }

    private void update() {

        if (i.getStringExtra("post") == null) return;

        post.setText(i.getStringExtra("post").replace(",", "\n"));

        edit.setText(i.getStringExtra("post"));

        Button look = (Button) findViewById(R.id.look);
        look.setVisibility(View.VISIBLE);
        look.setOnClickListener(this);
    }

    private boolean verifyText() {
        String content = edit.getText().toString();
        if (content != null && !content.equals("") && content.contains(",") && content.contains(" ")) return true;
        else {
            Utils.MessageBox(LolPostActivity.this, getResources().getString(R.string.post_error));
            return false;
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.save:
                if (Kin.ADD.equals(i.getAction())) {
                    // new
                    if (verifyText()) {
                        db.addPost(LolPostActivity.this, edit.getText().toString());
                        Utils.MessageBox(LolPostActivity.this, getResources().getString(R.string.post_success));
                        finish();
                    }

                } else {
                    // modify
                    if (verifyText()) {
                        db.updatePost(LolPostActivity.this, i.getIntExtra("id", 0), edit.getText().toString());
                        Utils.MessageBox(LolPostActivity.this, getResources().getString(R.string.post_success));
                        finish();
                    }
                }
                break;
            case R.id.look:

                db.updateConfigs(LolPostActivity.this, 0, 0, 0, 0, i.getIntExtra("id", -1));
                Utils.MessageBox(LolPostActivity.this, getResources().getString(R.string.post_success));
                finish();
                break;
        }
    }
}
